/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package shaman.storage.impl;

import java.awt.Frame;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import shaman.storage.api.Filter;

/**
 *
 * @author dmitriy
 */
public class CompositeFilter implements Filter {

    final List<Filter> filters;

    public CompositeFilter(List<Filter> filters) {
        this.filters = Collections.unmodifiableList(new ArrayList<Filter>(filters));
    }

    public FilterType getFilterType() {
        return FilterType.POSTPROCESSOR;
    }

    public String getName() {
        return "Composite";
    }

    public OutputStream getOutputStream(OutputStream baseStream) throws IOException {
        OutputStream lastStream=baseStream;
        ListIterator<Filter> li;
        for (li=filters.listIterator(filters.size()); li.hasPrevious(); )
        {
            Filter f=li.previous();
            lastStream=f.getOutputStream(lastStream);
        }
        return lastStream;
    }

    public InputStream getInputStream(InputStream baseStream) throws IOException {
        InputStream lastStream=baseStream;
        ListIterator<Filter> li;
        for (li=filters.listIterator(filters.size()); li.hasPrevious(); )
        {
            Filter f=li.previous();
            lastStream=f.getInputStream(lastStream);
        }
        return lastStream;
    }

    public boolean hasConfigForm() {
        return false;
    }

    public boolean showConfigForm(Frame parent) {
        throw new UnsupportedOperationException("Inapplicable to this type of filter");
    }

    public List<String> getPropertyNames() {
        return new LinkedList<String>();
    }

    public String getPropertyValue(String name) throws InvalidParameterException {
        throw new UnsupportedOperationException("Inapplicable to this type of filter");
    }

    public List<String> getPropertyValues(String name) throws InvalidParameterException {
        throw new UnsupportedOperationException("Inapplicable to this type of filter");
    }

    public void setPropertyValue(String name, String value) throws InvalidParameterException {
        throw new UnsupportedOperationException("Inapplicable to this type of filter");
    }

    public List<Filter> getFilters() {
        return filters;
    }

}
